package edu.gatech.cs2340.OregonTrail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import edu.gatech.cs2340.OregonTrail.EventSystem.ActorEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.FoodEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.IllnessEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.OxEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.WagonEvent;

public class GameDB {

	private static GameDB ref;
	private List<Item> itemList;
	private List<ActorEvent> eventList;
	
	private GameDB(){
		ItemLoader itemLoader = new ItemLoader();
		eventList = new ArrayList<ActorEvent>();
		try {
			itemLoader.loadItems("ItemXML.xml");
			itemList = itemLoader.getItems();
		} catch (SAXException e) {
			JOptionPane.showMessageDialog(null, "Hey, the parser broke. Fix it");
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			JOptionPane.showMessageDialog(null, "Hey, the parser broke. Fix it. The configuration messed up");
			e.printStackTrace();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Hey, the parser broke. Fix it. The file couldn't open");
			e.printStackTrace();
		}
		
		eventList.add(new IllnessEvent(this, eStatus.sick, "REPLACEME is ill."));
		eventList.add(new IllnessEvent(this, eStatus.dead, "REPLACEME has died."));
		eventList.add(new WagonEvent(this, getItemByName("A wagon wheel"), "Wagon is damaged, use REPLACEME " +
				"to fix it. "));
		eventList.add(new WagonEvent(this, getItemByName("A wagon axle"), "Wagon is damaged,  use REPLACEME " +
				"to fix it. "));
		eventList.add(new WagonEvent(this, getItemByName("A wagon tongue"), "Wagon is damaged, use REPLACEME " +
				"to fix it. "));
		eventList.add(new FoodEvent(this, 20, 50, "First Nation hunting party feels sorry for you, gives you REPLACEME pounds of food."));
		eventList.add(new FoodEvent(this, -1, -10, "Thieving Won Taeks strike in the night and steals REPLACEME pounds of food."));
		eventList.add(new OxEvent(this, 1, "An ox has died."));
	}
	
	public static GameDB getGameDB(){
		if(ref == null){
			ref = new GameDB();
		}

		return ref;
	}
	
	public List<Item> getItemList(){
		return itemList;
	}
	
	public Item getItemByName(String name){
		for(int i = 0; i < itemList.size(); i++){
			if(itemList.get(i).getItemName().equalsIgnoreCase(name)){
				return itemList.get(i);
			}
		}
		
		return null;
	}

	public List<ActorEvent> getEventList() {
		return this.eventList;
	}
	
}
